//
//
//
//   CMPS 294  Spring, 2000     Homework 6                  Ralph Coldewey
//
//   Date due --  2-Apr-2000             Date completed --  2-Apr-2000
//
//   Purpose: JDBC, JTable and GUI
//
//
//
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
 
public class Homework6 extends JFrame implements ActionListener,ItemListener {
   JTable tbl = null;
   JScrollPane scrollPane = null;
   JButton btnAdd = new JButton("Add"), btnSave = new JButton("Save"),
           btnDelete = new JButton("Delete"), btnReload = new JButton("Reload");
   JTextField strFilter = new JTextField(20);
   JPanel  pnButtons = new JPanel();
   Container c;

   private JMenuItem openItem;
   private JMenuItem saveItem;
   private JMenuItem asLabelsItem;
   private JMenuItem helpItem;
   private JMenuItem aboutItem;
   private JMenuItem exitItem;
   private JTextField filtFld;
   JCheckBox colBox[] = new JCheckBox[13];
   JButton go, go2;
   JButton close, close2;
   JDialog jd, jd2;
   JTextField enteredText[] = new JTextField[13];
   String cTitles[] = { "Company_Name", "LastName", "FirstName",
                         "Home No.", "Work No.", "EMail", "Pager", "Fax",
                         "Address", "City", "State", "Zip", "Memo" };

   boolean columnSelected[] = new boolean[13];
   private JTextField txtFld[] = new JTextField[13];

   JDBC2TableModel pTblMdl = new JDBC2TableModel();

   public Homework6() {
      super( "CS 294 Homework 6" );
      c = getContentPane();
      tbl = new JTable( pTblMdl );
      scrollPane = new JScrollPane(tbl); // No scrollpane, no column titles will be displayed.
      scrollPane.setPreferredSize( new Dimension( 800, 240 ) );
      JTableHeader header = tbl.getTableHeader();
      header.addMouseListener( pTblMdl.new ColumnListener( tbl ) );
      
      // make a menubar and add to the pane
      JMenuBar mbar = createMenuBar();
      setJMenuBar( mbar );
      c.add( new JButton( new ImageIcon( "picture.gif" ) ), BorderLayout.NORTH );
      c.add(scrollPane, BorderLayout.SOUTH /*CENTER*/);
      // make a subpanel for text fields, etc above the table
      JPanel subPanel = new JPanel();
      setUpSubPanel( subPanel );

      c.add( subPanel, BorderLayout.CENTER /*NORTH*/);

      // List selection listener to get selected row and display its memo field
      // when a row of the table is selected.
      ListSelectionModel rowSM = tbl.getSelectionModel();
      rowSM.addListSelectionListener( new ListSelectionListener() {
         public void valueChanged( ListSelectionEvent e ) {
            ListSelectionModel lsm = (ListSelectionModel)e.getSource();
            if( lsm.isSelectionEmpty() ) { }
            else {
               int row = tbl.getSelectedRow();
               for( int i = 0; i < 13; i++ )
                  if( pTblMdl.getValueAt( row, i ) == null ) txtFld[i].setText( " " );
                  else txtFld[i].setText( pTblMdl.getValueAt( row, i ).toString() );
            }
         }
      } );

      setSize(800, 500);
      show();
   }
 
   public void paint(Graphics g) {
      super.paint(g);
   }
 
   public static void main(String args[]) {
      Homework6 hw6 = new Homework6();

      hw6.addWindowListener( new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }} );
   }

// --------------------------- MENU BAR  ----------------------------------
   private JMenuBar createMenuBar() {
// sets up the menu bar
      JMenuBar mbar = new JMenuBar();
      System.out.println( "Entering createMenuBar" );
//--------------------- SAVE MENU -------------------------------
      JMenu saveMenu = new JMenu( "Save" );
      saveMenu.setMnemonic( 'S' );

      saveItem = new JMenuItem( "Save selected fields", 'f' );
      saveItem.addActionListener( this );
      saveMenu.add( saveItem );

      asLabelsItem = new JMenuItem( "Save as labels", 'l' );
      asLabelsItem.addActionListener( this );
      saveMenu.add( asLabelsItem );

      saveMenu.addSeparator();

      exitItem = new JMenuItem( "Exit", 'X' );
      exitItem.addActionListener( this );
      saveMenu.add( exitItem );

      mbar.add( saveMenu );
//---------------------------- FILE MENU ---------------------------------
      JMenu fileMenu = new JMenu( "File" );
      fileMenu.setMnemonic( 'F' );

      openItem = new JMenuItem( "Open file", 'O' );
      openItem.addActionListener( this );
      fileMenu.add( openItem );

      mbar.add( fileMenu );
//-------------------------- HELP MENU --------------------------------
      JMenu helpMenu = new JMenu( "Help" );
      helpMenu.setMnemonic( 'H' );
      helpItem = new JMenuItem( "Help", 'H' );
      helpMenu.add( helpItem );
      helpItem.addActionListener( this );
      aboutItem = new JMenuItem( "About ...", 'A' );
      helpMenu.add( aboutItem );
      aboutItem.addActionListener( this );
      mbar.add( helpMenu );

      return mbar;
   }
// --------- subpanel for text fields -- GridBagLayout --------------------
   private void setUpSubPanel( JPanel subPanelMain ) {

      GridBagLayout gb = new GridBagLayout();
      GridBagConstraints constr = new GridBagConstraints();
      JPanel subPanel = new JPanel( gb );

      constr.gridwidth = 1;
      constr.anchor = GridBagConstraints.EAST;
      JLabel coLabel = new JLabel( "Company" );
      gb.setConstraints( coLabel, constr );
      subPanel.add( coLabel );
      constr.fill = GridBagConstraints.HORIZONTAL;
      constr.gridwidth = 3;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[0] = new JTextField( 20 );
      txtFld[0].addActionListener( this );
      subPanel.add( txtFld[0], constr );

      constr.fill = GridBagConstraints.NONE;
      constr.gridwidth = 1;
      constr.anchor = GridBagConstraints.EAST;
      JLabel stLabel = new JLabel( "Street" );
      gb.setConstraints( stLabel, constr );
      subPanel.add( stLabel );
      constr.gridwidth = 3;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[8] = new JTextField( 14 );
      txtFld[8].addActionListener( this );
      subPanel.add( txtFld[8], constr );

      constr.gridwidth = 1;
      constr.anchor = GridBagConstraints.EAST;
      JLabel oPhLabel = new JLabel( "Office Ph#" );
      gb.setConstraints( oPhLabel, constr );
      subPanel.add( oPhLabel );
      int origwidth = constr.gridwidth;
      constr.gridwidth = GridBagConstraints.REMAINDER;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[4] = new JTextField( 8 );
      txtFld[4].addActionListener( this );
      subPanel.add( txtFld[4], constr );

      constr.anchor = GridBagConstraints.EAST;
      constr.gridwidth = 1;                            //origwidth;
      JLabel lnLabel = new JLabel( "Last Name" );
      gb.setConstraints( lnLabel, constr );
      subPanel.add( lnLabel );
      constr.anchor = GridBagConstraints.WEST;
      txtFld[1] = new JTextField( 8 );
      txtFld[1].addActionListener( this );
      subPanel.add( txtFld[1], constr );

      constr.anchor = GridBagConstraints.EAST;
      JLabel fnLabel = new JLabel( "First Name" );
      gb.setConstraints( fnLabel, constr );
      subPanel.add( fnLabel );
      constr.anchor = GridBagConstraints.WEST;
      txtFld[2] = new JTextField( 8 );
      txtFld[2].addActionListener( this );
      subPanel.add( txtFld[2], constr );

      constr.anchor = GridBagConstraints.EAST;
      JLabel ctLabel = new JLabel( "City" );
      gb.setConstraints( ctLabel, constr );
      subPanel.add( ctLabel );
      constr.gridwidth = 3;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[9] = new JTextField( 14 );
      txtFld[9].addActionListener( this );
      subPanel.add( txtFld[9], constr );

      constr.gridwidth = 1;
      constr.anchor = GridBagConstraints.EAST;
      JLabel hPLabel = new JLabel( "Home Ph#" );
      gb.setConstraints( hPLabel, constr );
      subPanel.add( hPLabel );
      constr.gridwidth = GridBagConstraints.REMAINDER;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[3] = new JTextField( 8 );
      txtFld[3].addActionListener( this );
      subPanel.add( txtFld[3], constr );

      constr.gridwidth = 1;
      constr.gridheight = GridBagConstraints.RELATIVE;
      constr.anchor = GridBagConstraints.EAST;
      JLabel memoLabel = new JLabel( "Memo" );
      gb.setConstraints( memoLabel, constr );
      subPanel.add( memoLabel );
      constr.gridwidth = 3;
      constr.gridheight = 2;
      constr.fill = GridBagConstraints.BOTH;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[12] = new JTextField( 18 );
      txtFld[12].addActionListener( this );
      subPanel.add( txtFld[12], constr );

      constr.fill = GridBagConstraints.NONE;
      constr.fill = GridBagConstraints.VERTICAL;
      constr.gridheight = 1;
      constr.anchor = GridBagConstraints.EAST;
      constr.gridwidth = 1;
      JLabel staLabel = new JLabel( "State" );
      gb.setConstraints( staLabel, constr );
      subPanel.add( staLabel );
      constr.anchor = GridBagConstraints.WEST;
      txtFld[10] = new JTextField( 6 );
      txtFld[10].addActionListener( this );
      subPanel.add( txtFld[10], constr );

      constr.fill = GridBagConstraints.HORIZONTAL;
      constr.anchor = GridBagConstraints.EAST;
      constr.gridwidth = 1;
      JLabel zipLabel = new JLabel( "Zip" );
      gb.setConstraints( zipLabel, constr );
      subPanel.add( zipLabel );
      constr.anchor = GridBagConstraints.WEST;
      txtFld[11] = new JTextField( 6 );
      txtFld[11].addActionListener( this );
      subPanel.add( txtFld[11], constr );

      constr.fill = GridBagConstraints.NONE;
      constr.gridwidth = 1;
      constr.anchor = GridBagConstraints.EAST;
      JLabel pagLabel = new JLabel( "Pager" );
      gb.setConstraints( pagLabel, constr );
      subPanel.add( pagLabel );
      constr.gridwidth = GridBagConstraints.REMAINDER;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[6] = new JTextField( 8 );
      txtFld[6].addActionListener( this );
      subPanel.add( txtFld[6], constr );

      constr.gridwidth = 1;
      constr.anchor = GridBagConstraints.EAST;
      JLabel emailLabel = new JLabel( "EMail" );
      gb.setConstraints( emailLabel, constr );
      subPanel.add( emailLabel );
      constr.gridwidth = 3;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[5] = new JTextField( 14 );
      txtFld[5].addActionListener( this );
      subPanel.add( txtFld[5], constr );

      constr.gridwidth = 1;
      constr.anchor = GridBagConstraints.EAST;
      JLabel faxLabel = new JLabel( "Fax" );
      gb.setConstraints( faxLabel, constr );
      subPanel.add( faxLabel );
      constr.gridwidth = GridBagConstraints.REMAINDER;
      constr.anchor = GridBagConstraints.WEST;
      txtFld[7] = new JTextField( 8 );
      txtFld[7].addActionListener( this );
      subPanel.add( txtFld[7], constr );

      subPanelMain.add( subPanel, BorderLayout.WEST );

      GridBagLayout gb2 = new GridBagLayout();
      GridBagConstraints constr2 = new GridBagConstraints();
      JPanel subPanel2 = new JPanel( gb2 );

      constr2.gridwidth = GridBagConstraints.REMAINDER;
      JLabel filtLabel = new JLabel( "Filtering Records" );
      gb2.setConstraints( filtLabel, constr2 );
      subPanel2.add( filtLabel );

      constr2.fill = GridBagConstraints.HORIZONTAL;
      filtFld = new JTextField( 11 );
      filtFld.addActionListener( this );
      subPanel2.add( filtFld, constr2 );

      constr2.gridwidth = 1;
      btnAdd.addActionListener(this);
      btnAdd.setToolTipText("Add new record");
      subPanel2.add( btnAdd, constr2 );

      constr2.gridwidth = GridBagConstraints.REMAINDER;
      btnDelete.addActionListener(this);
      btnDelete.setToolTipText("Delete record from database");
      subPanel2.add( btnDelete, constr2 );

      constr2.gridwidth = 1;
      btnReload.addActionListener(this);
      btnReload.setToolTipText("Reload entire table");
      subPanel2.add( btnReload, constr2 );

      constr2.gridwidth = GridBagConstraints.REMAINDER;
      btnSave.addActionListener(this);
      btnSave.setToolTipText("Save batch update");
      subPanel2.add( btnSave, constr2 );

      subPanelMain.add( subPanel2, BorderLayout.EAST );
   }


// ------------------------- ACTION LISTENERS --------------------------------
   public void actionPerformed(ActionEvent e) {
      Object o = e.getSource();
      if ( o == filtFld ) {
         pTblMdl.requery( filtFld.getText().trim() );
         return;
      }
      if ( o.equals( openItem ) ) {
         // to open a view of a text file
         TextView viewer = new TextView( "Text Viewer" );
         return;
      }
      if ( o.equals( saveItem ) ) {
         // to save selected columns of data to text file
         jd = new JDialog( this, "Column Selection" );
         jd.getContentPane().setLayout( new GridLayout( 16, 1 ) );

         JLabel note = new JLabel( "Select columns to save:" );
         jd.getContentPane().add( note );
         for( int i = 0; i < 13; i++ ) {
            colBox[i] = new JCheckBox( cTitles[i] );
            colBox[i].addItemListener( this );
            jd.getContentPane().add( colBox[i] );
         }
         go = new JButton( "Save data" );
         go.addActionListener( this );
         jd.getContentPane().add( go );
         close = new JButton( "Close" );
         close.addActionListener( this );
         jd.getContentPane().add( close );

         jd.setSize( 400, 400 );
         jd.show();
         return;
      }

      if( o.equals( go ) ) {
         // a button on the saveItem window to make it happen
         pTblMdl.makeTextFile( columnSelected );
         return;
      }
      if( o.equals( close ) ) {
         // a button on the saveItem window to close it
         jd.dispose();
         return;
      }
      if ( o.equals( asLabelsItem ) ) {
         // option to make a text file formatted as address labels
         pTblMdl.makeLabelFile();
         return;
      }
      if (o.equals( helpItem ) ) {        // help menu
         String msg;
         msg = " 1. Click on column title to sort by that column." +
               " Click again to reverse sort order.\n" +
               " 2. To filter list, type filter string and enter.\n" +
               "                 ";

         JOptionPane.showMessageDialog(null, msg, "Help",
         JOptionPane.INFORMATION_MESSAGE);
         return;
      }
      if (o.equals( aboutItem ) ) {                   // about this program
         String msg;
         msg = "        CS 350 Homework 6, Spring 2000        \n"  +
               "                by Ralph Coldewey             \n"  +
               "Demonstrates JDBC, JTable and GUI capabilities\n"  +
               "including GridBagLayout." ;
         JOptionPane.showMessageDialog(null, msg, "About",
                                       JOptionPane.INFORMATION_MESSAGE);
         return;
      }
      if (o.equals( exitItem ) ) System.exit( 0 );

      if( o.equals( btnAdd ) ) {
         // this is the Add button for adding a new record to database
         // it creates a window for entering the data
         System.out.println( "Add button was pressed" );
         jd2 = new JDialog( this, "Add Data Entry" );
         jd2.getContentPane().setLayout( new GridLayout( 15, 2 ) );
         for( int i = 0; i < 13; i++ ) {
            jd2.getContentPane().add( new JLabel( cTitles[i] ) );
            enteredText[i] = new JTextField( 20 );
            jd2.getContentPane().add( enteredText[i] );
         }
         go2 = new JButton( "Insert data" );
         go2.addActionListener( this );
         jd2.getContentPane().add( go2 );
         close2 = new JButton( "Close" );
         close2.addActionListener( this );
         jd2.getContentPane().add( close2 );

         jd2.setSize( 350, 400 );
         jd2.show();
         return;
      }
      if( o.equals( go2 ) ) {
         // this button goes with the add item option to make it happen
         String values[] = new String[13];
         for( int i = 0; i < 13; i++ ) values[i] = enteredText[i].getText();
         pTblMdl.addRow( values );
         repaint();
         return;
      }
      if( o.equals( close2 ) ) {
         // this button goes with the add item option to close that window
         System.out.println( "close button was pressed" );
         jd2.dispose();
         return;
      }
      if( o.equals( btnDelete ) ) {
         // this is the Delete button to delete the selected row
         System.out.println( "Delete button was pressed" );
         int row = tbl.getSelectedRow();
         pTblMdl.removeRow( row );
         tbl.setRowSelectionInterval( row, row );
         return;
      }
      if( o.equals( btnSave ) ) {
         // this is the Save Changes button
         System.out.println( "Save Changes button was pressed." );
         JOptionPane.showMessageDialog( null,
            "Batch changes not implemented. Changes are done as they happen." ,
            "Save Changes Message", JOptionPane.INFORMATION_MESSAGE );
         return;
      }
      if( o.equals( btnReload ) ) {
         // this is the Reload button to reload data from database
         pTblMdl.reload();
         return;
      }
      for( int i = 0; i < 13; i++ ) {
      // this puts data into the text boxes from the selected row of table
         if( o.equals( txtFld[i] ) ) {
            int row = tbl.getSelectedRow();
            pTblMdl.setValueAt( txtFld[i].getText(), row, i );
            pTblMdl.fireTableDataChanged();
            return;
         }
      }

   }

   public void itemStateChanged(ItemEvent e) {
// this watches checkboxes to see if they are checked or unchecked
      int columnID = 0;
      Object source = e.getItemSelectable();
      for( int i = 0; i < 13; i++ )
         if (source == colBox[i] ) {
            columnID = i;
         }
      if (e.getStateChange() == ItemEvent.DESELECTED) {
            // was deselected
         columnSelected[ columnID ] = false;
      } else {
         // was selected
         columnSelected[ columnID ] = true;
      }

   }
}

